package cn.smile.business.basic.impl;

import cn.smile.bean.dto.basic.BasicItemDTO;
import cn.smile.bean.entity.basic.ItemToBasic;
import cn.smile.bean.form.basic.item.QueryBasicItemForm;
import cn.smile.business.basic.IBasicItemService;
import cn.smile.business.basic.IItemToBasicService;
import cn.smile.commons.constant.NumberConstant;
import cn.smile.commons.domain.BaseServiceImpl;
import cn.smile.repository.basic.ItemToBasicMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Slf4j
@Service
public class ItemToBasicServiceImpl extends BaseServiceImpl<ItemToBasicMapper, ItemToBasic> implements IItemToBasicService {

    @Resource
    private IBasicItemService basicItemService;

    @Override
    public void saveCorrelation(String itemId, List<String> basicList) {
        log.info("[ItemToBasicServiceImpl].[saveCorrelation] ------> 保存消费单关联基础项目集合 Start, itemId = {}", itemId);
        //先删除之前的
        this.delCorrelation(itemId);
        int insertNum = NumberConstant.ZERO;
        if (!CollectionUtils.isEmpty(basicList)) {
            for (String basicId : basicList) {
                ItemToBasic entity = new ItemToBasic();
                entity.setItemId(itemId);
                entity.setBasicId(basicId);
                if (super.create(entity)) {
                    insertNum++;
                }
            }
        }
        log.info("[ItemToBasicServiceImpl].[saveCorrelation] ------> 保存消费单关联基础项目集合 End, insertNum = {}", insertNum);
    }

    @Override
    public void delCorrelation(String itemId) {
        log.info("[ItemToBasicServiceImpl].[delCorrelation] ------> 删除消费单关联基础项目集合 Start, itemId = {}", itemId);
        int delNum = NumberConstant.ZERO;
        List<ItemToBasic> correlationList = baseMapper.selectList(new QueryWrapper<ItemToBasic>().lambda().eq(ItemToBasic::getItemId, itemId).eq(ItemToBasic::getDeleted, Boolean.FALSE));
        if (!CollectionUtils.isEmpty(correlationList)) {
            for (ItemToBasic entity : correlationList) {
                if (super.delById(entity.getId())) {
                    delNum++;
                }
            }
        }
        log.info("[ItemToBasicServiceImpl].[delCorrelation] ------> 删除消费单关联基础项目集合 End, delNum = {}", delNum);
    }

    @Override
    public Map<String, List<BasicItemDTO>> getByItemIds(List<String> itemIds) {
        log.info("[ItemToBasicServiceImpl].[getByItemId] ------> 根据消费单ID查询关联基础项目ID集合 Start,");
        Map<String, List<BasicItemDTO>> dataMap = Maps.newHashMap();
        if (!CollectionUtils.isEmpty(itemIds)) {
            List<ItemToBasic> correlationList = baseMapper.selectList(new QueryWrapper<ItemToBasic>().lambda().in(ItemToBasic::getItemId, itemIds).eq(ItemToBasic::getDeleted, Boolean.FALSE));
            if (!CollectionUtils.isEmpty(correlationList)) {
                Set<String> basicSet = new HashSet<>(NumberConstant.EIGHT);
                correlationList.forEach(entity -> basicSet.add(entity.getBasicId()));
                QueryBasicItemForm form = new QueryBasicItemForm();
                form.setIds(Lists.newArrayList(basicSet));
                List<BasicItemDTO> basicItemList = basicItemService.getList(form);
                Map<String, BasicItemDTO> basicItemMap = Maps.newHashMap();
                if (!CollectionUtils.isEmpty(basicItemList)) {
                    basicItemList.forEach(item -> basicItemMap.put(item.getId(), item));
                }

                for (ItemToBasic entity : correlationList) {
                    if (!dataMap.containsKey(entity.getItemId())) {
                        dataMap.put(entity.getItemId(), Lists.newArrayList());
                    }
                    if (basicItemMap.containsKey(entity.getBasicId())) {
                        dataMap.get(entity.getItemId()).add(basicItemMap.get(entity.getBasicId()));
                    }
                }
            }
        }

        log.info("[ItemToBasicServiceImpl].[getByItemId] ------> 根据消费单ID查询关联基础项目ID集合 End");
        return dataMap;
    }

    @Override
    public List<String> getByBasicId(String basicId) {
        log.info("[ItemToBasicServiceImpl].[getByBasicId] ------> 根据基础项目ID查询关联消费单ID集合 Start, basicId = {}", basicId);
        Set<String> itemIdList = new HashSet<>(NumberConstant.EIGHT);
        List<ItemToBasic> correlationList = baseMapper.selectList(new QueryWrapper<ItemToBasic>().lambda().eq(ItemToBasic::getBasicId, basicId).eq(ItemToBasic::getDeleted, Boolean.FALSE));
        if (!CollectionUtils.isEmpty(correlationList)) {
            correlationList.forEach(entity -> itemIdList.add(entity.getItemId()));
        }
        log.info("[ItemToBasicServiceImpl].[getByBasicId] ------> 根据基础项目ID查询关联消费单ID集合 End, itemIdList.size = {}", itemIdList.size());
        return Lists.newArrayList(itemIdList);
    }
}
